//
//  JiuViewController.m
//  UI_豆瓣app
//
//  Created by 151129-49宋强 on 15/12/28.
//  Copyright © 2015年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "JiuViewController.h"
#import "JiuCollectionViewCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"

@interface JiuViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger i;
@property (nonatomic, assign)NSInteger j;
@end

@implementation JiuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
  
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake(118, (667 - 64 - 49 - 4) / 3);
//    layout.minimumInteritemSpacing =   554 - 480 / 3;
    layout.minimumLineSpacing = 1;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
    
    [self.view addSubview:collectionView];
    [self data];
    
//    [collectionView registerClass:[JiuCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [collectionView registerNib:[UINib nibWithNibName:@"JiuCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    
    
    
    
}
- (void)data
{
    self.dataArr = [NSMutableArray array];
   NSArray *arr =  [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"]];
    for (NSDictionary *dic in arr) {
        MovieModel *m = [[MovieModel alloc] init];
        [m setValuesForKeysWithDictionary:dic];
        m.largeImage = dic[@"images"][@"large"];
        [_dataArr addObject:m];
    }
    
    
    //NSLog(@"%@",_dataArr);
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    JiuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    MovieModel *m = _dataArr[indexPath.row];
       [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.largeImage]];
    cell.nameLabel.text = m.title;
    cell.ratingLabel.text = m.rating;
    
    NSMutableArray *arr = [NSMutableArray array];
    NSString *star = @"red";
    NSString *unstar = @"gray";
    NSString *halfstar = @"half";
   NSInteger temp = m.rating.integerValue;
    for (int i = 0; i < 5; i++) {
        if (i < temp / 2) {
            [arr insertObject:star atIndex:i];
        }else{
            [arr insertObject:unstar atIndex:i];
        }
        
    };
    if (temp % 2 >= 1) {
        [arr replaceObjectAtIndex:temp / 2 withObject:halfstar];
    }
    cell.imgView1.image = [UIImage imageNamed:arr[0]];
    cell.imgView2.image = [UIImage imageNamed:arr[1]];
    cell.imgView3.image = [UIImage imageNamed:arr[2]];
    cell.imgView4.image = [UIImage imageNamed:arr[3]];
    cell.imgView5.image = [UIImage imageNamed:arr[4]];
    
    
    
 
    
    
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
